var Tasklist;

/***************************************************************
 *
 *   Tasklist
 *
 ***************************************************************/
(Tasklist = function (tasklist_data) { 

    var elem = document.getElementById(tasklist_data.id);
    if(elem) {
        elem.outerHTML = "";
        delete elem;
    }
    elem = document.createElement('li');
    elem.id = tasklist_data.id;
    elem.tasklist = this;

    this.init(elem, tasklist_data.title);
})
.prototype = {
    init: function (elem, title) {

        this.elem = elem;
        this.title = getTasklistTitle(title);
        this.strcolor = getTasklistColor(title);

        this.label = this.createTasklistLabel();
        this.itemCount = this.createItemCount();
        this.tasksBox = this.createTasksBox(this.itemCount);
        this.itemAddButton = this.createAddItemButton(this.tasksBox);

        this.setStyleElement();
    },

    setColor: function(elem) {
        if(this.strcolor) 
            elem.style.color = this.strcolor;
        else
            elem.style.color = "#ccc";
    },

    getColor: function() {
        return (this.strcolor) ? " @color:#"+this.strcolor : "";
    },

    color: function() {
        return this.elem.style.color;
    },

    id: function() {
        return this.elem.id;
    },

    makeTitle: function() {
        return this.title + this.getColor();
    },

    setStyleElement: function() {
        var tasklist = this;
        this.setColor(tasklist.elem);
        tasklist.elem.className = "tasklist_label";
        tasklist.elem.onclick = function(evt) {
            doubleclick(this, evt, function () {
                tasklist.tasksBox.toggle();
            }, function () {});
        };

        this.elem.appendChild(this.label);
        this.elem.appendChild(this.itemAddButton);
        this.elem.appendChild(this.itemCount);
        this.elem.appendChild(this.tasksBox);
    },

    foldToggleList: function(evt) {
        var tasklist = this;
        doubleclick(tasklist.elem, evt, function () {
            tasklist.tasksBox.toggle();
        }, function () {});
    },

    createTasklistLabel: function() {
        var tasklist = this;
        var label = createEditableLabel(this.title, function() {
            if(label.value != tasklist.title) {
                tasklist.title = label.value;
                tasklist.request_update();
            }
            label.blur();
        });
        this.setColor(label);
        label.style.textTransform = "uppercase";
        return label;
    },

    createItemCount: function() {
        var item_count = document.createElement('text');
        item_count.count = 0;
        item_count.innerText = "(" + item_count.count + ")";
        item_count.addCount = function() {
            this.innerText = "("+ (++this.count) + ")";
        };
        item_count.delCount = function() {
            this.innerText = "(" + (--this.count) + ")";
        };

        return item_count;
    },

    addCount: function() {
        this.itemCount.addCount();
    },

    delCount: function() {
        this.itemCount.delCount();
    },

    createAddItemButton: function(tasksBox) {
        var tasklist = this;
        var item_add = document.createElement('a');
        item_add.href = "#";
        item_add.innerText = "+";
        item_add.onclick = function (evt) {
            evt.stopPropagation();
            var task = new Task(Task.newItem(), tasklist);
            tasksBox.addTask(task.elem);
            task.request_insert();
        }

        return item_add;
    },

    createTasksBox: function(itemCount) {
        var tasklist = this;
        var ul = document.createElement('ul');
        ul.style.display = "block";
        ul.addTask = function(task) {
            this.appendChild(task);
            tasklist.addCount();
        };
        ul.toggle = function() {
            if (this.style.display == "block")
                this.style.display = "none";
            else
                this.style.display = "block";
        };

        return ul;
    },

    getElementById: function(id) {
        return (id) ? Task.getTasksBox(id) : this.tasksBox;
    },

    request_list: function(dates) {

        var tasklist = this;
        var end = getFormatedDate(dates.endTime) + 'T23:59:59.999Z';
        var req_data = {
            'tasklist': this.elem.id,
            'showCompleted': false,
            'dueMax': end
        };

        var req = gapi.client.tasks.tasks.list(req_data);
        req.execute(function(res) {
            if (res.hasOwnProperty("items")) {
                res.items.forEach(function (val, index, theArray) {

                    var task = new Task(val, tasklist);
                    var target = tasklist.getElementById(val.parent);
                    target.addTask(task.elem);

                    if (run_on_gadget) {
                        gadgets.window.adjustHeight();
                    }
                });
            } 
        });
    },

    request_update: function() {
        var req_title = this.makeTitle();
        var req_data = {
            tasklist: this.id(),
            resource: { 
                title: req_title,
                id: this.id()
            }
        };

        var tasklist = this;
        var req = gapi.client.tasks.tasklists.update(req_data);
        console.log("update tasklist : " + req_title);
        req.execute(function (res) {
            console.log(res.result);
        });
    },
};

